Test Failed
Pull Request — master (#2)
by Luís
03:32 queued 01:49
created

???.capitalize   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 1
1
export default {
2
    /**
3
     * Add basepath if isn't complete
4
     * Consider a complete url when it contains '//'
5
     *
6
     * Examples of complete urls:
7
     * http://google.com
8
     * https://google.com
9
     * //google.com
10
     *
11
     * Examples of incomplete urls:
12
     * google.com
13
     * google
14
     * users/create
15
     * /users/create
16
     *
17
     * @param {String} url
18
     * @param {String} basePath basepath to be used, with protocol
19
     * @returns {String}
20
     * 
21
     */
22
    resolveUrl: function (url, basePath = "") {
23
        if (url.indexOf("//") === -1) {
24
            return basePath + url;
25
        }
26
27
        return url;
28
    },
29
30
    /**
31
     * Makes string capitalized
32
     * @param {String} string
33
     * @returns {string}
34
     */
35
    ucfirst: function (string) {
36
        "use strict";
37
38
        string = string.toLocaleLowerCase();
39
        return string.charAt(0).toLocaleUpperCase() + string.substr(1);
40
    },
41
42
    /**
43
     * @see App.helpers.string.ucfirst
44
     */
45
    capitalize: function (string) {
46
        return this.ucfirst(string);
47
    },
48
49
    /**
50
     * Makes every word from string capitalized
51
     * @param {String} string
52
     * @returns {string}
53
     */
54
    ucwords: function (string) {
55
        return string.split(" ").map(word => this.ucfirst(word)).join(" ");
56
    },
57
58
    /**
59
     * Will crop the text to fit the maxLength provided. Will try to not break any words
60
     * and add "..." on the end of the string
61
     * 
62
     * @param text
63
     * @param maxLength
64
     * @returns {String}
65
     */
66
    excerpt: function (text, maxLength) {
67
        if (maxLength !== parseInt(maxLength, 10) || maxLength < 1) {
68
            throw "maxLength must me an integer greater than 0";
69
        }
70
71
        if (typeof text !== "string") {
72
            throw "text must be string";
73
        }
74
75
        if (text.length > maxLength) {
76
            const exploded = text.split(" ");
77
            let counter = 0;
78
            let i = 0;
79
            let response = [];
80
81
            for (i = 0; i < exploded.length; i++) {
82
                if (counter + exploded[i].length <= maxLength || i === 0) {
83
                    response.push(exploded[i]);
84
                    counter += exploded[i].length;
85
                } else {
86
                    break;
87
                }
88
            }
89
90
            text = response.join(" ") + "...";
91
        }
92
93
        return text;
94
    },
95
96
    /**
97
     * generares a UUID
98
     * Ref: http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
99
     * @returns {String}
100
     */
101
    uuid: function () {
102
        "use strict";
103
104
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
105
            var r = Math.random()*16|0, v = c === "x" ? r : (r&0x3|0x8);
106
            return v.toString(16);
107
        });
108
    }
109
}
110